*****************************************************************************
*SET UP
*****************************************************************************
global root_dir = ""
di "$root_dir"

do "${root_dir}/Code/setup.do"

****************************************************************************************
*Import Data
****************************************************************************************
use "$workdir/chat_level", clear

merge 1:1 $chatid using "$workdir/temp_topics"
drop if _merge==2
drop _merge

merge m:1 topic using "$workdir/topic_rank"
assert _merge!=2
drop _merge

gen topic_grpA1=(topic=="payroll & taxes" | topic=="account access & management")
gen topic_grpA2=(topic_rank>=3 & topic_rank<=7 & (topic!="other" & topic!="unsure"))
gen topic_grpA3=(topic_rank>=8 & topic_rank<=16 & (topic!="other" & topic!="unsure"))
gen topic_grpA4=(topic_rank>=17 & topic_rank!=. & (topic!="other" & topic!="unsure"))
replace topic_grpA4=1 if topic=="other"

forvalues i = 1/4 {
    gen topicgrpA_treat_`i' = (topic_grpA`i' == 1) * postXevertreated
}

save "$dlwork/chat_topics", replace

use "$dlwork/chat_topics", clear

forvalues i=1/4 {
    sum chat_duration_minutes if pre_treat==1 & topic_grpA`i'==1
    scalar baseline_mean_`i' = r(mean)
}

label var topicgrpA_treat_1 "Q1 (Most Common Overall)"
label var topicgrpA_treat_2 "Q2"
label var topicgrpA_treat_3 "Q3"
label var topicgrpA_treat_4 "Q4 (Least Common Overall)"

capture drop chat_dur_t
gen chat_dur_t = .

forvalues i=1/4 {
    replace chat_dur_t = chat_duration_minutes/baseline_mean_`i' if topic_grpA`i' ==1
}

reghdfe chat_dur_t topicgrpA_treat_* if topic_treat_unsure==0 & topic!="" & topic!="other", absorb(chat_ym agent_id agent_tenure_months topic_grpA*) cluster(agent_id)
estadd ysumm 
estimates store est_pct
*************************************************************************************
* FIGURE VIII PANEL A - AI IMPACT BY CHAT TOPIC -
* Impact of AI on Chat Duration, by Overall Topic Frequency
*************************************************************************************
coefplot est_pct, drop(_cons) vertical base omitted msymbol(D) mcolor(midblue) msize(large) ciopts(color(midblue) recast(rcap)) ///
coeflabels(topicgrpA_treat_1="Payroll/Account (50%)" topicgrpA_treat_2="Next 25%" topicgrpA_treat_3="Next 15%" topicgrpA_treat_4="Last 10%") /// 
ytitle("Percentage Change in Chat Duration",  size(medsmall)) xtitle("Topic Frequency" , size(medsmall)) xlabel(, angle(45) labsize(medsmall)) /// 
ylabel( , labsize($ylabelsize) nogrid) $graphbackground
graph export "${figures_dir}/figureVIII_panelA.eps", as(eps) preview(on) replace  fontface($graphfont)


****************************************************************************************
*WITHIN-AGENT TOPIC FREQUENCY
****************************************************************************************
bys topic agent_id: gen cluster_freq=_N if topic!="unsure" & topic!="other" & topic!=""
replace cluster_freq=-1*cluster_freq
xtile topicW_xtile4 = cluster_freq, nq(4)
assert topicW_xtile4 == . if topic_unsure == 1 | topic=="" | topic_other == 1

forvalues i = 1/4 {
    gen topicW4_treat_`i' = (topicW_xtile4 == `i') * postXevertreated
}

capture drop chat_dur_t_indiv
gen chat_dur_t_indiv = .

forvalues i=1/4 {
    sum chat_duration_minutes if pre_treat==1 & topicW_xtile4==`i'
    scalar baseline_mean_`i' = r(mean)
    replace chat_dur_t_indiv= chat_duration_minutes/baseline_mean_`i' if topicW_xtile4==`i'
}

label var topicW4_treat_1 "Q1 (Most Common within Agent)"
label var topicW4_treat_2 "Q2"
label var topicW4_treat_3 "Q3"
label var topicW4_treat_4 "Q4 (Least Common within Agent)"


reghdfe chat_dur_t_indiv topicW4_treat_* if topic_treat_unsure==0 & topic!="" & topic!="other", absorb(chat_ym agent_id agent_tenure_months i.topicW_xtile4 topic_grpA1 topic_grpA2 topic_grpA3 topic_grpA4) cluster(agent_id)
estadd ysumm
estimates store est_t_indiv

*************************************************************************************
* FIGURE VIII PANEL A - AI IMPACT BY CHAT TOPIC -
* Impact of AI on Chat Duration, by Overall Topic Frequency
*************************************************************************************

coefplot est_t_indiv, drop(_cons) vertical base omitted msymbol(D) mcolor(midblue) msize(large) ciopts(color(midblue) recast(rcap)) coeflabels(topicW4_treat_1="Q1 (Most Common)"  topicW4_treat_2="Q2" topicW4_treat_3="Q3" topicW4_treat_4="Q4 (Least Common)", angle(45))  ///
 ytitle("Percentage Change in Chat Duration ", size(medsmall)) xtitle("Agent Chat Frequency Quartile", size($xtitlesize)) ///
 xlabel(, angle(45) labsize(medsmall) nogrid) ylabel( , labsize($ylabelsize) nogrid) $graphbackground 
graph export "${figures_dir}/figureVIII_panelB.eps", as(eps) preview(on) replace  fontface($graphfont)

